package com.itheima.wms.controller;

import com.itheima.wms.controller.dto.GoodsPageDto;
import com.itheima.wms.pojo.Goods;
import com.itheima.wms.pojo.GoodsType;
import com.itheima.wms.pojo.OwnerPageDto;
import com.itheima.wms.service.GoodsService;
import com.itheima.wms.utils.PageBean;
import com.itheima.wms.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private final GoodsService goodsService;

    @GetMapping("/pageDetail")
    public Result<PageBean<Goods>> goodsPage(GoodsPageDto goodsPageDto) {
        PageBean<Goods> pageBean = goodsService.goodsPage(goodsPageDto);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result addGoods(@RequestBody Goods goods) {
        goodsService.add(goods);
        return Result.success("添加成功");
    }


    @DeleteMapping
    public Result<String> delete(@RequestParam("ids[]") List<Long> ids) {
        boolean isEmpty = goodsService.removeByIds(ids);
        if (isEmpty) {
            return Result.success("操作成功");
        }
        return Result.fail("删除失败");
    }

    @GetMapping(path = "/{id}")
    public Result getGoodsTypeById(@PathVariable Long id) {
        Goods good = goodsService.getgoodById(id);
        return Result.success(good);
    }


    @PutMapping
    public Result updateGoodsType(@RequestBody Goods goods) {
        goodsService.update(goods);
        return Result.success("修改成功");
    }
}
